package demo1;

import java.util.ArrayList;
import java.util.List;

public class BinaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }
    // 前序遍历
   public void preOrder(TreeNode root){
        if(root == null){
            return ;
        }
       System.out.print(root.val + "  ");
       preOrder(root.left);
       preOrder(root.right);
    }
    // 中序遍历
   public void inOrder(TreeNode root){
        if(root == null){
            return;
        }
        inOrder(root.left);
       System.out.print(root.val + "  ");
       inOrder(root.right);
    }
    // 后序遍历
    public List postOrder(TreeNode root){
        List<Character> list = new ArrayList<>();
        if(root == null){
            return list;
        }
        List<Character> list1 = postOrder(root.left);
        list.addAll(list1);
        List<Character> list2 = postOrder(root.right);
        list.addAll(list2);
        System.out.print(root.val + "  ");
        list.add(root.val);
        return list;
    }
    //计算树的节点数
    int count = 0;
    public int size(TreeNode root){
        if(root == null){
            return 0;
        }
        count++;
        size(root.left);
        size(root.right);
        return count;
    }
    public int size2(TreeNode root){
        if(root == null){
            return 0;
        }
        return size2(root.left) + size2(root.right) + 1;
    }
    //获取叶子节点的个数
   public int leafSize = 0;
    public int leafNode (TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            leafSize++;
        }
        leafNode(root.left);
        leafNode(root.right);
        return leafSize;
    }


    public int leafNode2(TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }
        return leafNode2(root.left)+ leafNode2(root.right);
    }

    //求第K层节点个数
    public int getKLevelNodeCount(TreeNode root,int k){
        if(root == null){
            return 0;
        }
        if(k == 1){
            return 1;
        }
        return getKLevelNodeCount(root.right,k-1) + getKLevelNodeCount(root.left,k-1);
    }

    //二叉树的高度
    public int getHeight(TreeNode root){
        if(root == null){
            return 0;
        }
        //左树的高度
        int leftH = getHeight(root.left);
        int rightH = getHeight(root.right);
        return (leftH > rightH ? leftH : rightH) + 1;

    }
}
